﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [AppService]
    public class ContentService
    {
        private readonly ContentRepository contentDal;
        private readonly ContentContentRepository contentContentDal;
        private readonly ICacheManager cacheManager;
        private readonly string cacheKey;
        private readonly string cacheListKey;
        public ContentService(ContentRepository contentRepository, ContentContentRepository  contentContentRepository, ICacheManager CacheManager)
        {
            cacheManager = CacheManager;
            contentDal = contentRepository;
            contentContentDal = contentContentRepository;
            cacheKey = CacheConst.CACHE_KEY_ARTICLE;
            cacheListKey = CacheConst.CACHE_KEY_ARTICLELIST;
        }

        /// <summary>
        /// 前台查询一条
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ViewContentAndContent> GetAsync(int id)
        {
            ViewContentAndContent content = cacheManager.Get<ViewContentAndContent>(cacheKey + id);
            if (content == null)
            {
                content = await contentDal.GetAsync(id);
                cacheManager.Set(cacheKey + id, content, TimeSpan.FromHours(10));//缓存2秒
            }
            await contentDal.IncHits(id);//浏览+1
            return content;
        }

        /// <summary>
        /// 后台编辑一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EditContentAndContent> GetEditAsync(int? id)
        {
            return await contentDal.GetEditAsync(id);
        }

        /// <summary>
        /// 按CID获取全部列表 后台
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewContentAndCate>> GetListByCidAdminAsync(ContentQuery contentQuery)
        {
            contentQuery.Orderby = "Id desc";
            return await contentDal.GetListAsync(contentQuery);
        }

        /// <summary>
        /// 获取全部列表  前台 所有列表 博客首页  获取 status=1 列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewContentAndCate>> GetListAsync(ContentQuery contentQuery)
        {
            contentQuery.Status = 1;
            contentQuery.Orderby = "Id desc";
            return await contentDal.GetListAsync(contentQuery);
        }

        /// <summary>
        /// 按CID获取全部列表 获取 status=1 列表  前台
        /// </summary>
        /// <param name="id"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewContentAndCate>> GetListByCidAsync(string cid, int page, int limit)
        {
            ContentQuery contentQuery = new()
            {
                CurrentPage = page,
                Cid = cid,
                Limit = limit,
                Orderby = "Id desc",
                Status = 1
            };
            return await contentDal.GetListAsync(contentQuery);
        }

        /// <summary>
        /// 搜索列表 前台
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public async Task<Page<ViewContentAndCate>> ContentSearchPageList(int page, int limit, string keyword)
        {
            return await contentDal.SearchListAsync(page, limit, keyword);

        }

        /// <summary>
        /// 按CID获取指定条数列表 前台
        /// </summary>
        /// <returns></returns>
        public async Task<List<ViewContentAndCate>> CxuuList(ContentQuery contentQuery)
        {
            List<ViewContentAndCate> content = cacheManager.Get<List<ViewContentAndCate>>(cacheListKey + contentQuery.CacheName);
            if (content == null)
            {
                content = await contentDal.GetListByLimtAsync(contentQuery);
                if (contentQuery.CacheName != null)
                    cacheManager.Set(cacheListKey + contentQuery.CacheName, content, TimeSpan.FromSeconds(contentQuery.CacheTime));//缓存
            }
            return content;
        }


        public virtual async Task<Content> InsertContentAndContentAsync(Content content, ContentContent  contentContent)
        {
            Content contentRes = await contentDal.InsertContentAndContentAsync(content, contentContent);
            cacheManager.Remove(cacheListKey + contentRes.Cid);
            return contentRes;
        }

        public virtual async Task<int> UpdateContentAndContentAsync(Content content, ContentContent contentContent)
        {
            cacheManager.Remove(cacheKey + content.Id);
            return await contentDal.UpdateContentAndContentAsync(content, contentContent);
        }

        public virtual async Task<int> UpdateContentAttAsync(int id, int status, string att)
        {
            return await contentDal.UpdateContentAttAsync(id, status, att);
        }


        /// <summary>
        /// 删除一条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public virtual async Task<int> DelContentAndContentAsync(int id)
        {
            if (await contentDal.DeleteByPriKeyAsync(id) > 0)
            {
                await contentContentDal.DeleteByPriKeyAsync(id);
                cacheManager.Remove(cacheKey + id);
                return 1;
            }
            return 0;
        }
    }
}
